package com.coolxi.service;

import com.coolxi.dao.FlowerMapper;
import com.coolxi.entity.Flower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Eoly on 2017/6/12.
 */
@Service("flowerBean")
public class FlowerServiceImpl implements FlowerService {

    @Autowired
    FlowerMapper flowerMapper;

    @Override
    public void addFlower(Flower flower) {
        Integer result = flowerMapper.insertSelective(flower);
        if (result!=1){
            throw new RuntimeException("添加花失败");
        }
    }

    @Override
    public List<Flower> findAllFlowers() {
        return flowerMapper.selectFlowers(null,null);
    }

    @Override
    public void editFlower(Flower flower) {
        if (flower.getFlowerId()==null){
            throw new RuntimeException("未指定修改花的id");
        }
        Integer result = flowerMapper.updateByPrimaryKeySelective(flower);
        if (result!=1){
            throw new RuntimeException("修改花失败");
        }
    }

    @Override
    public List<Flower> findFlowers(Integer offset, Integer flowerSize) {
        return flowerMapper.selectFlowers(offset,flowerSize);
    }

    @Override
    public void deleteFlower(Integer id) {
        Integer result = flowerMapper.deleteByPrimaryKey(id);
        if (result!=1){
            throw new RuntimeException("删除失败");
        }
    }
}
